package com.molin.mapTest.hashMapTest;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

public class Demo1 {
    public static void main(String[] args) {
        HashMap<Student, String> studentStringHashMap = new HashMap<>();
        studentStringHashMap.put(new Student("xiaowang", 23), "molin001");
        studentStringHashMap.put(new Student("xiaoli", 26), "molin002");
        studentStringHashMap.put(new Student("xiaohei", 25), "molin003");
        studentStringHashMap.put(new Student("xiaowang", 23), "molin001");
        studentStringHashMap.put(new Student("xiaoli", 26), "molin002");
        studentStringHashMap.put(new Student("xiaohei", 25), "molin003");

        Set<Student> keys = studentStringHashMap.keySet();
        for (Student key : keys) {
            System.out.println(key + " === " + studentStringHashMap.get(key));
        }

        System.out.println("============================");
        Set<Map.Entry<Student, String>> entries = studentStringHashMap.entrySet();
        for (Map.Entry<Student, String> entry : entries) {
            System.out.println(entry.getKey() + " === " + entry.getValue());

        }
        System.out.println("==========================");
        studentStringHashMap.forEach((Student student,String id)->{
            System.out.println(student.toString()+" === "+id);
        });

    }
}

class Student {
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Student() {
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age &&
                Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}